package com.ssh.cp.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.ssh.cp.entity.User;
import com.ssh.cp.idao.IUserDao;


@Repository  
public class UserDao implements IUserDao{  
    @Resource(name="sessionFactory")   
    private SessionFactory sessionFactory;  
    
    
    public void addUser(User user ) {  
        Session session = sessionFactory.getCurrentSession();
        
        try {
            session.save(user);  
        }catch(RuntimeException re) {			
			throw re;
		}
    }

	
	
	public List<?> loginUser(User user) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession(); 
		@SuppressWarnings("rawtypes")
		Query s=session.createQuery("from user u where u.username=? and u.password=?");
		s.setParameter(0, user.getUsername());		
		s.setParameter(1,user.getPassword());
		return s.list();
	}  
    
	public List<?> listUser(){
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
		
 		try {
		query=session.createQuery("from user where roleid=2");

          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
 		
 		
		return list;
		
		
		
	}



	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		Session session =sessionFactory.getCurrentSession();
		Query<?> query=null;
		try {
			query=session.createQuery("update user set nickname=?,birthday=?,gmt_modified=? where userid=? ");
			query.setParameter(0,user.getNickname());
			query.setParameter(1,user.getBirthday());
			query.setParameter(2,user.getGmt_modified());
			query.setParameter(3,user.getUserid());
			int i=query.executeUpdate();
			System.out.println("=====i:  "+i);
		}catch(RuntimeException re) {			
			throw re;
		}
		
		
		
		
	}



	@Override
	public boolean checkPassword(int userid, String oldpassword) {
		// TODO Auto-generated method stub
		Session session =sessionFactory.getCurrentSession();
		Query<?> query=null;
		List<?> list=null;
		try {
			query=session.createQuery("from user where userid=? and password=?");
			query.setParameter(0,userid);
			query.setParameter(1,oldpassword);
			list=query.list();
			
		}catch(RuntimeException re) {			
			throw re;
		}
		if(list.isEmpty())
			return false;
		else
			return true;
	}



	@Override
	public boolean changePassword(int userid, String newpassword) {
		// TODO Auto-generated method stub
		Session session =sessionFactory.getCurrentSession();
		Query<?> query=null;
		int i=0;
		try {
			query=session.createQuery("update user set password=? where userid=?");
			query.setParameter(0,newpassword);
			query.setParameter(1,userid);
			i=query.executeUpdate();
			
		}catch(RuntimeException re) {			
			throw re;
		}
		if(i<1)
			return false;
		else
			return true;
	}

/*
 * 检测用户角色是否为1（管理员）,是则返回真，否则假
 * (non-Javadoc)
 * @see com.ssh.cp.dao.idao.IUserDao#checkRole(int)
 */

	@Override
	public boolean checkRole(int userid) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
		
 		try {
		query=session.createQuery("from user where userid=?");
		query.setParameter(0, userid);
          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
 		User u=(User)list.get(0);
 		if(u.getRoleid()==1)
 			return true;
 		else
 			return false;
	}



	@Override
	public void forbidUser(int theuserid, int forbid) {
		// TODO Auto-generated method stub
		Session session =sessionFactory.getCurrentSession();
		Query<?> query=null;
		try {
			query=session.createQuery("update user set forbid=? where userid=?");
			query.setParameter(0,forbid);
			query.setParameter(1,theuserid); 
			
			query.executeUpdate();
		}catch(RuntimeException re) {			
			throw re;
		}
	}



	@Override
	public boolean existUserName(String username) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
		
 		try {
		query=session.createQuery("from user where username=?");
		query.setParameter(0, username);
          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
 		
 		if(list.size()>0)
 			return true;
 		else
 			return false;
	}

	
	
  
}  
